package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.slider.RangeSlider;
import com.google.android.material.slider.Slider;

import java.util.List;

import cn.lrwjz.androidguide.R;

public class SliderActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slider);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Slider 示例");
        }

        // 设置连续滑块
        Slider continuousSlider = findViewById(R.id.continuousSlider);
        continuousSlider.addOnChangeListener((slider, value, fromUser) -> {
            if (fromUser) {
                Toast.makeText(this, 
                    String.format("连续滑块值: %.1f", value), 
                    Toast.LENGTH_SHORT).show();
            }
        });

        // 设置离散滑块
        Slider discreteSlider = findViewById(R.id.discreteSlider);
        discreteSlider.addOnChangeListener((slider, value, fromUser) -> {
            if (fromUser) {
                Toast.makeText(this, 
                    String.format("离散滑块值: %.0f", value), 
                    Toast.LENGTH_SHORT).show();
            }
        });

        // 设置范围滑块
        RangeSlider rangeSlider = findViewById(R.id.rangeSlider);
        rangeSlider.addOnChangeListener((slider, value, fromUser) -> {
            if (fromUser) {
                List<Float> values = rangeSlider.getValues();
                Toast.makeText(this, 
                    String.format("范围滑块值: %.1f - %.1f", 
                        values.get(0), values.get(values.size() - 1)), 
                    Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 